﻿Imports System.IO
Imports PowerSong.SongDatabase.Legacy.Items

Namespace ExportImport

    ''' <summary>
    ''' Represents the facility to use import data from a Presenter database.
    ''' </summary>
    Public Class PresenterImport
        Inherits BaseImport

        Private FIncludeSubFolders As Boolean = False

        ''' <summary>
        ''' Initializes a new instance of the <see cref="PresenterImport" /> class.
        ''' </summary>
        ''' <param name="database">The database.</param>
        ''' <param name="includeSubFolders">True if you want to include sub folders as well as files contained in the source folder.</param>
        Public Sub New(ByVal database As Database, ByVal includeSubFolders As Boolean)
            MyBase.New(database)
            FIncludeSubFolders = includeSubFolders
        End Sub

        ''' <summary>
        ''' Imports the data contained at the specified source folder location into the PowerSong database.
        ''' </summary>
        ''' <param name="sourceFolderLocation">The source folder location.</param>
        Public Overrides Sub Import(ByVal sourceFolderLocation As String)

            ' First create a default category if one does not exist
            Dim CategoryID As Guid = Database.Categories.GetOrCreateDefaultCategory()

            ' Start the import
            Database.StartTransaction()
            ImportFiles(sourceFolderLocation, CategoryID)
            Database.EndTransaction()

            ' Save the main database and recreate the song index
            MyBase.Database.Save()

        End Sub

        Private Sub ImportFiles(ByVal location As String, ByVal defaultCategoryID As Guid)

            ' Loop through each file at the source location
            For Each FileName As String In Directory.GetFiles(location, "*.txt")

                Dim LineNumber As Integer = 1
                Dim VerseNumber As Integer = 0
                Dim Song As New SongItem
                Dim CurrentVerse As String = String.Empty

                Using SourceFile As New StreamReader(New FileStream(FileName, FileMode.Open))
                    While Not SourceFile.EndOfStream

                        Dim Line As String = SourceFile.ReadLine

                        If LineNumber = 1 Then
                            Song.Title = Line
                        Else

                            If Line.Length > 1 AndAlso Line.Chars(0) = "."c Then

                                Dim Rest As String = Line.Substring(2).Trim
                                Select Case Line.Chars(1)

                                    Case "i"c, "I"c : Song.CopyrightLine = Rest
                                    Case "s"c, "S"c ' Sequence (ie: 1C2C)
                                    Case Else
                                        If CurrentVerse <> "" Then
                                            Song.Parts.Add(CurrentVerse)
                                            CurrentVerse = ""
                                            VerseNumber += 1
                                        End If

                                End Select

                            Else
                                If Line <> "" Then CurrentVerse += Line + Environment.NewLine
                            End If

                            If (Line.Length = 0 OrElse SourceFile.EndOfStream) AndAlso CurrentVerse <> "" Then
                                Song.Parts.Add(CurrentVerse)
                                CurrentVerse = ""
                                VerseNumber += 1
                            End If

                        End If

                        LineNumber += 1

                    End While

                End Using
                Song.Categories.Add(defaultCategoryID)
                MyBase.Database.Songs.AddSong(Song)

            Next

            ' Recurse if necessary
            If FIncludeSubFolders Then
                For Each FolderName As String In Directory.GetDirectories(location)
                    ImportFiles(FolderName, defaultCategoryID)
                Next
            End If

        End Sub

    End Class

End Namespace